import { Component, OnInit } from '@angular/core';
import { News } from '../../model/news';
import { NewsService } from '../../service/news.service';
import { Router, ActivatedRoute } from '@angular/router';
import { ConfigService } from '../../service/config.service';

@Component({
  selector: 'app-newslist',
  templateUrl: './newslist.component.html',
  styleUrls: ['./newslist.component.css']
})
export class NewslistComponent implements OnInit {
  n1:Array<News>;
  HOST:string;
  constructor(
    private newsservice:NewsService,
    private router:Router,
    private routerinfo:ActivatedRoute,
    private config:ConfigService
  ) { 
    this.HOST=config.host+"/public/"
  }

  ngOnInit() {
    this.loadNewsList();
  }
  
  private loadNewsList(){
    this.n1=new Array();
    this.newsservice.getNewsList().then((data:any)=>{
    this.n1=data;
    })
  }
  

  addNews(){
    this.router.navigate(['editnews','add']);
   }

  updateNews(id:string){
     this.router.navigate(['editnews',id]);
   }

  deleteNews(id:string){
    this.newsservice.deleteNews(id)
    .then((flag:boolean)=>{
     if(flag==true){
  this.loadNewsList();
     }
     else{
      alert("删除异常，请刷新后重试");
     }
    })
  }

  toDetail(id:string){
    this.router.navigate(['detail',id]);
    }


    selectedFile: string;
    onFileChanged(event) {
      this.selectedFile = event.target.files[0];
    }
    uploadNewsCover(newsid:string){
      this.newsservice.uploadNewsCoverPic(newsid,this.selectedFile)
      .then((data:any)=>{
      if(data.filename){
        this.loadNewsList()
      }
      //console.dir(data);
      })
     }

  
}
